package sms

import (
	"github.com/aaronwong1989/gosms/codec"
	"github.com/aaronwong1989/gosms/codec/cmpp"
	"github.com/aaronwong1989/gosms/codec/smgp"

	"goskeleton/app/global/variable"
	"goskeleton/app/http/validator/api/sms"
	"goskeleton/app/http/validator/core/interf"
)

func SendSms(dto interface{}) (rtCode int, err error) {

	vmt := dto.(interf.ValidatorInterface)
	mt := vmt.(*sms.TextSms)
	msc := SplitPhones(mt.Phones)
	if len(msc[0]) > 0 {
		tmp := *mt
		tmp.Phones = msc[0]
		pdus := NewCmppSms(&tmp)
		// TODO SEND PDU
		variable.ZapLog.Sugar().Infof("sms: %s", pdus)
	}
	return
}

func NewCmppSms(mt *sms.TextSms) []codec.Pdu {
	mts := cmpp.NewSubmit(mt.Phones, mt.Content, cmpp.MtAtTime(mt.AtTime.Time()), cmpp.MtMsgLevel(uint8(mt.Priority)))
	pdus := make([]codec.Pdu, len(mts))
	for i := 0; i < len(mts); i++ {
		pdus[i] = mts[i]
	}
	return pdus
}

func NewSmgpSms(mt *sms.TextSms) []codec.Pdu {
	mts := smgp.NewSubmit(mt.Phones, mt.Content, smgp.MtOptions{
		AtTime:   mt.AtTime.Time(),
		Priority: byte(mt.Priority),
	})
	pdus := make([]codec.Pdu, len(mts))
	for i := 0; i < len(mts); i++ {
		pdus[i] = mts[i]
	}
	return pdus
}
